#include "TileInfoPanel.hpp"
TileInfoPanel::TileInfoPanel():Panel(0,290, 200, 215, Panel::Static){
    
	//Set panel title.
    title.setPosition(5, 2);
    title.setString(10, sf::Color::White, sf::Text::Underlined, "");
    add(&title);
	
	//Add ui elements for all layers
    for(int i = 0; i < LAYERS; i++){
        tileSprites[i].setRect(5, 20 + i *32 + 2*i, 32, 32);

        spriteNames[i].setPosition(42, 37 + i *32 + 2*i);
        spriteNames[i].setString(10, sf::Color::White, sf::Text::Regular, "");
        add(&spriteNames[i]);

        layers[i].setPosition(42, 20 + i *32 + 2*i);
        layers[i].setString(10, sf::Color::White, sf::Text::Regular, "Layer " + StringHelper::itos(i));
        add(&layers[i]);
        add(&tileSprites[i]);
    }
    
	//Add property info elements
    properties.setPosition(5, 49 + (LAYERS-1)*32 + 2*LAYERS);
    properties.setString(10, sf::Color::White, sf::Text::Regular, "Properties");

	propertySprite.width = 32;
	propertySprite.height = 32;
    propertyIcon.setRect(5, 20 + (LAYERS) *32 + 2*(LAYERS), 32, 32);
    propertyIcon.setIcon(&propertySprite);
    
    add(&properties);
    add(&propertyIcon);
}

/** 
* Sets the tile the panel displays info about.
*/
void TileInfoPanel::setTile(Tile& tile, int tileRow, int tileCol){
    
	//Update tile location.
	title.setString("Tile Information (" + StringHelper::itos(tileCol) + "," + StringHelper::itos(tileRow) +")");
    //Update all layer information.
	for(int i = 0; i < LAYERS; i++){
		//Show tile sprite.
        if(tile[i] != NO_SPRITE){
            BaseSprite* layerSprite = spriteManager->getSprite(tile[i]);
            if(layerSprite->type == BaseSprite::st_Static){
                spriteNames[i].setString(std::string(layerSprite->name) + " " 
                + StringHelper::itos(((StaticSprite*)layerSprite)->getSprite().getTextureRect().width) + "x"
                + StringHelper::itos(((StaticSprite*)layerSprite)->getSprite().getTextureRect().height));
            }

            tileSprites[i].setIcon(layerSprite);
            spriteNames[i].rect.left = 42;
            layers[i].rect.left = 42;
        }else{
            spriteNames[i].setString("Blank");
            spriteNames[i].rect.left = 5;
            layers[i].rect.left = 5;
            tileSprites[i].setIcon(0);
        }
    }
    
    if(tile.properties){
        properties.setString("Properties: " + StringHelper::itos(tile.properties));
        properties.rect.left = 42;

        if(tile.properties & Tile::t_BlockTop)
            propertySprite.render(editorSprites->getSprite("blockedtop"));
        if(tile.properties & Tile::t_BlockLeft)
            propertySprite.render(editorSprites->getSprite("blockedleft"));
        if(tile.properties & Tile::t_BlockRight)
            propertySprite.render(editorSprites->getSprite("blockedright"));
        if(tile.properties & Tile::t_BlockBottom)
            propertySprite.render(editorSprites->getSprite("blockedbottom"));
    }else{
        properties.setString("Properties: None");
		propertySprite.clear();
        properties.rect.left = 5;
    }
}